package yzy.ecps.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import yzy.ecps.model.BtcBrand;
import yzy.ecps.model.BtcFeature;
import yzy.ecps.model.BtcGoods;
import yzy.ecps.model.BtcGoodsClob;
import yzy.ecps.model.BtcParaValue;
import yzy.ecps.model.BtcSku;
import yzy.ecps.model.BtcSpecValue;
import yzy.ecps.query.condition.GoodsQueryCondition;
import yzy.ecps.service.BrandService;
import yzy.ecps.service.FeatureService;
import yzy.ecps.service.GoodsService;
import yzy.ecps.utils.PageInfo;

/**
 * 商品控制器
 * @author YZY
 *
 */
@Controller
@RequestMapping("/item")
public class GoodsController {
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private BrandService brandService;
	@Autowired
	private FeatureService featureService;
	
	/**
	 * 商品条件筛选，分页列表展示页面
	 * @param model
	 * @param gqc
	 * @return
	 */
	@RequestMapping("/list.do")
	public String list(Model model,GoodsQueryCondition gqc){
		List<BtcBrand> bList = brandService.findAll();
		model.addAttribute("bList", bList);
		PageInfo page = goodsService.getPageInfo(gqc);
		model.addAttribute("page", page);
		model.addAttribute("gqc", gqc);
		return "item/list";
	}
	
	/**
	 * 添加商品页面
	 * @return
	 */
	@RequestMapping("toAdd.do")
	public String toAdd(Model model){
		//提供品牌列表，供页面选择
		List<BtcBrand> bList = brandService.findAll();
		model.addAttribute("bList", bList);
		
		//TODO:提供类别列表，供页面选择
		
		//提供基本参数列表，供页面选择
		List<BtcFeature> commList = featureService.findCommonByCatgoryId(1001L);
		model.addAttribute("commList", commList);
		
		//提供规格属性列表，供页面创建商品最小销售单元
		List<BtcFeature> specList = featureService.findSpecByCatgoryId(1001L);
		model.addAttribute("specList", specList);
		return "item/addItem";
	}
	
	/**
	 * 添加商品
	 * @param goods 商品基本信息对象
	 * @param goodsClob 商品大字段信息对象
	 * @param divNum 页面中添加的销售单元对应的DIV的个数
	 * @param request
	 * @return
	 */
	@RequestMapping("/add.do")
	public String add(BtcGoods goods,BtcGoodsClob goodsClob,int divNum,HttpServletRequest request){
		
		//根据普通参数属性的ID从页面获取录入的参数值列表
		List<BtcParaValue> paraValues = getPVS(request);
		
		//根据页面销售单元创建的DIV个数，获取有效的销售单元列表
		List<BtcSku> skus = getSkus(divNum, request);
		
		goodsService.saveGoods(goods, goodsClob, skus, paraValues);
		return "redirect:list.do";
	}
	
	/**
	 * 商品审核列表页面
	 * @param model
	 * @param gqc
	 * @return
	 */
	@RequestMapping("/listAudit.do")
	public String listAudit(Model model,GoodsQueryCondition gqc){
		List<BtcBrand> bList = brandService.findAll();
		model.addAttribute("bList", bList);
		PageInfo page = goodsService.getPageInfo(gqc);
		model.addAttribute("page", page);
		model.addAttribute("gqc", gqc);
		return "item/listAudit";
	}
	
	/**
	 * 商品上下架
	 * @param goodsId
	 * @param showStatus
	 * @return
	 */
	@RequestMapping("/showGoods.do")
	public String showGoods(Long goodsId,Integer showStatus){
		goodsService.showGoods(goodsId, showStatus);
		if(showStatus == 0)
			return "redirect:list.do?showStatus=1&auditStatus=1";
		return "redirect:list.do?showStatus=0&auditStatus=1";
	}
	
	/**
	 * 商品审核
	 * @param goodsId
	 * @param auditStatus
	 * @param notes
	 * @return
	 */
	@RequestMapping("/auditGoods")
	public String auditGoods(Long goodsId,Integer auditStatus,String notes){
		goodsService.auditGoods(goodsId, auditStatus, notes);
		return "redirect:listAudit.do?showStatus=1&auditStatus=0";
	}
	
	/**
	 * 根据表单录入的有效数据创建商品普通参数值列表
	 * @param request
	 * @return
	 */
	private List<BtcParaValue> getPVS(HttpServletRequest request){
		List<BtcParaValue> paraValues = new ArrayList<>();
		List<BtcFeature> commList = featureService.findCommonByCatgoryId(1001L);
		//遍历普通参数属性
		for (BtcFeature f : commList) {
			Long featureId = f.getFeatureId();
			
			//inputType:3=checkbox(多选）2=radio(单选） 1=select(单选)
			//如果是多选表单
			if(f.getInputType()==3){
				String value = "";
				//获取当前表单录入的多个值，并用 ,连接
				String[] values = request.getParameterValues(featureId+"");
				if(values != null){					
					BtcParaValue pv = new BtcParaValue();
					for (String v : values) {
						if(v != null && !v.trim().equals("")){
							pv.setFeatureId(featureId);
							value += v+",";
						}
					}
					value = value.substring(0,value.length()-1);
					if(pv.getFeatureId() != null){
						pv.setParaValues(value);
						paraValues.add(pv);
					}
				}
			}
			//单选表单
			else{
				String value = request.getParameter(featureId+"");
				if(value != null && !value.trim().equals("")){
					BtcParaValue pv = new BtcParaValue();
					pv.setFeatureId(featureId);
					pv.setParaValues(value);
					paraValues.add(pv);
				}
			}
		}
		
		return paraValues;
	}
	
	/**
	 * 根据表单录入的有效数据创建商品销售单元列表
	 * @param divNum
	 * @param request
	 * @return
	 */
	private List<BtcSku> getSkus(int divNum,HttpServletRequest request){
		List<BtcSku> skus = new ArrayList<>();
		
		//skuType showStatus sort skuPrice marketPrice stockInventory skuUpperLimit sku location
		for (int i = 1; i <= divNum; i++) {
			String marketPrice = request.getParameter("marketPrice"+i);
			String stockInventory = request.getParameter("stockInventory"+i);
			//页面表单中上面两项为必填项，如果这两项的值不为空，则认为是有效的录入
			if(marketPrice != null && stockInventory != null){
				BtcSku sku = new BtcSku();
				sku.setMarketPrice(new BigDecimal(marketPrice));
				sku.setSkuInventory(new Integer(stockInventory));
				String skuType = request.getParameter("skuType"+i);
				String showStatus = request.getParameter("showStatus"+i);
				String sort = request.getParameter("sort"+i);
				String skuPrice = request.getParameter("skuPrice"+i);
				String skuUpperLimit = request.getParameter("skuUpperLimit"+i);
				String skuNum = request.getParameter("sku"+i);
				
				sku.setSkuType(new Integer(skuType));
				sku.setShowStatus(new Integer(showStatus));
				sku.setSort(new Integer(sort));
				sku.setSkuPrice(new BigDecimal(skuPrice));
				sku.setUpperLimit(new Integer(skuUpperLimit));
				sku.setSkuNum(skuNum);
				
				//添加对应的规格属性值
				sku.setSpecValues(getSpecs(i, request));
				
				skus.add(sku);
			}
		}
		
		return skus;
	}
	
	/**
	 * 获取表单中商品销售单元对应的规格属性值列表
	 * @param n 页面提交的DIV序号
	 * @param request
	 * @return
	 */
	private List<BtcSpecValue> getSpecs(int n,HttpServletRequest request){
		List<BtcSpecValue> specValues = new ArrayList<>();
		List<BtcFeature> specList = featureService.findSpecByCatgoryId(1001L);
	
		for (BtcFeature f : specList) {
			Long fId = f.getFeatureId();
			String value = request.getParameter(fId+"specradio"+n);
			if(value != null && !value.trim().equals("")){
				BtcSpecValue sv = new BtcSpecValue();
				sv.setFeatureId(fId);
				sv.setSpecValue(value);
				specValues.add(sv);
			}
		}
		return specValues;
	}
	
	/**
	 * 发布商品
	 * @param goodsId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/publish.do")
	public String publish(Long goodsId) throws Exception{
		goodsService.publishGoods(goodsId,"333");
		return "redirect:list.do?showStatus=0&auditStatus=1";
	}
}
